import 'package:sqflite/sqflite.dart';

class Item{
  int id;
  String name;
  String accountName;
  String accountPwd;
  String email;
  String phoneNum;
  String note;

  Map<String,dynamic> toMap(){
    var map = new Map<String,dynamic>();
    map['id']=id;
    map['name']=name;
    map['accountName']=accountName;
    map['accountPwd']=accountPwd;
    map['email']=email;
    map['phoneNum']=phoneNum;
    map['note']=note;
    return map;
  }

  static Item fromMap(Map<String,dynamic> map){
    Item item = new Item();
    item.id=map['id'];
    item.name=map['name'];
    item.accountName=map['accountName'];
    item.accountPwd=map['accountPwd'];
    item.email=map['email'];
    item.phoneNum=map['phoneNum'];
    item.note=map['note'];
    return item;
  }

  static List<Item> fromMapList(dynamic mapList){
    List<Item> list = new List(mapList.length);
    for(int i=0;i<mapList.length;i++){
      list[i]=fromMap(mapList[i]);
    }
    return list;
  }
}

class DatabaseHelper {
  static final DatabaseHelper _instance = DatabaseHelper.internal();
  factory DatabaseHelper() => _instance;
  final String tableName = "table_pwd";

  final String id='id';
  final String name='name';
  final String accountName='accountName';
  final String accountPwd='accountPwd';
  final String email='email';
  final String phoneNum='phoneNum';
  final String note='note';

  static Database _db;

  Future<Database> get db async {
    if (_db != null) {
      return _db;
    }
    _db = await initDb();
    return _db;
  }

  DatabaseHelper.internal();

  initDb() async {
    var databasesPath = await getDatabasesPath();
    String path = databasesPath+'/sqflite.db';
    print('数据库路径'+path);
    var ourDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return ourDb;
  }

  //创建数据库表
  void _onCreate(Database db, int version) async {
    await db.execute(
        "create table $tableName($id integer primary key,"
            "$name text not null ,"
            "$accountName text not null ,"
            "$accountPwd text not null ,"
            "$email text not null ,"
            "$phoneNum text not null ,"
            "$note text not null )");
    print("Table is created");
  }

//插入
  Future<int> saveItem(Item item) async {
    var dbClient = await db;
    int res = await dbClient.insert("$tableName", item.toMap());
    print(res.toString());
    return res;
  }

  //查询
  Future<List> getTotalList() async {
    var dbClient = await db;
    var result = await dbClient.rawQuery("SELECT * FROM $tableName ");
    return result.toList();
  }

  //查询总数
  Future<int> getCount() async {
    var dbClient = await db;
    return Sqflite.firstIntValue(await dbClient.rawQuery(
        "SELECT COUNT(*) FROM $tableName"
    ));
  }

//按照id查询
  Future<Item> getItem(int id) async {
    var dbClient = await db;
    var result = await dbClient.rawQuery("SELECT * FROM $tableName WHERE id = $id");
    if (result.length == 0)
      return null;
    return Item.fromMap(result.first);
  }


  //清空数据
  Future<int> clear() async {
    var dbClient = await db;
    return await dbClient.delete(tableName);
  }


  //根据id删除
 Future<Null> deleteItem(int id) async {
    var dbClient = await db;
    await dbClient.rawQuery("DELETE  FROM $tableName WHERE id = $id");
  }

  //修改
  Future<int> updateItem(Item item) async {
    var dbClient = await db;
    return await dbClient.update("$tableName", item.toMap(),
        where: "$id = ?", whereArgs: [item.id]);
  }

  //关闭
  Future close() async {
    var dbClient = await db;
    return dbClient.close();
  }
}